<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择课程类型" clearable>
          <el-option
              v-for="dict in course_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="班级" prop="classroom">
        <el-select v-model="queryParams.classroom" placeholder="请选择班级" clearable>
          <el-option
              v-for="dict in class_room"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['course:course:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['course:course:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['course:course:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['course:course:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="courseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="名称" align="center" prop="name"/>
      <el-table-column label="介绍" align="center" prop="description"/>
      <el-table-column label="课程类型" align="center" prop="type">
        <template #default="scope">
          <dict-tag :options="course_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="人数" align="center" prop="number"/>
      <el-table-column label="班级" align="center" prop="classroom">
        <template #default="scope">
          <dict-tag :options="class_room" :value="scope.row.classroom"/>
        </template>
      </el-table-column>


      <el-table-column label="老师" align="center" prop="userName"/>
      <el-table-column label="学分" align="center" prop="score"/>
      <el-table-column label="周一" align="center" prop="monday">
        <template #default="scope">
          <dict-tag :options="which_class" :value="scope.row.monday"/>
        </template>
      </el-table-column>
      <el-table-column label="周二" align="center" prop="tuesday">
        <template #default="scope">
          <dict-tag :options="which_class" :value="scope.row.tuesday"/>
        </template>
      </el-table-column>
      <el-table-column label="周三" align="center" prop="wednesday">
        <template #default="scope">
          <dict-tag :options="which_class" :value="scope.row.wednesday"/>
        </template>
      </el-table-column>
      <el-table-column label="周四" align="center" prop="thursday">
        <template #default="scope">
          <dict-tag :options="which_class" :value="scope.row.thursday"/>
        </template>
      </el-table-column>
      <el-table-column label="周五" align="center" prop="friday">
        <template #default="scope">
          <dict-tag :options="which_class" :value="scope.row.friday"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="course_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['course:course:edit']">修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['course:course:remove']">删除
          </el-button>
          <el-button link type="primary" icon="Plus" @click="handleSelect(scope.row)"
                     v-hasPermi="['course:course:select']">选课
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改课程对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="courseRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="介绍" prop="description">
          <el-input v-model="form.description" placeholder="请输入介绍"/>
        </el-form-item>
        <el-form-item label="课程类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择课程类型">
            <el-option
                v-for="dict in course_type"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="人数" prop="number">
          <el-input v-model="form.number" placeholder="请输入人数"/>
        </el-form-item>
        <el-form-item label="班级" prop="classroom">
          <el-select v-model="form.classroom" placeholder="请选择班级">
            <el-option
                v-for="dict in class_room"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="老师" prop="teacherId">
          <!--          <el-input v-model="form.teacherId" placeholder="请输入老师" />-->
          <el-select v-model="form.teacherId" placeholder="请选择">
            <el-option
                v-for="item in teacherList"
                :key="item.userId"
                :label="item.userName"
                :value="item.userId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学分" prop="score">
          <el-input v-model="form.score" placeholder="请输入学分"/>
        </el-form-item>
        <el-form-item label="周一" prop="monday">
          <el-select v-model="form.monday" placeholder="请选择周一">
            <el-option
                v-for="dict in which_class"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="周二" prop="tuesday">
          <el-select v-model="form.tuesday" placeholder="请选择周二">
            <el-option
                v-for="dict in which_class"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="周三" prop="wednesday">
          <el-select v-model="form.wednesday" placeholder="请选择周三">
            <el-option
                v-for="dict in which_class"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="周四" prop="thursday">
          <el-select v-model="form.thursday" placeholder="请选择周四">
            <el-option
                v-for="dict in which_class"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="周五" prop="friday">
          <el-select v-model="form.friday" placeholder="请选择周五">
            <el-option
                v-for="dict in which_class"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
                v-for="dict in course_status"
                :key="dict.value"
                :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Course">
import {ref, computed} from 'vue';
import {listCourse, getCourse, delCourse, addCourse, updateCourse, listTeacher,selectCourse} from "@/api/course/course";

const {proxy} = getCurrentInstance();
const {
  course_type,
  which_class,
  class_room,
  course_status
} = proxy.useDict('course_type', 'which_class', 'class_room', 'course_status');

const courseList = ref([]);
let teacherList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    type: null,
    classroom: null,
    teacherId: null,
  },
  rules: {
    status: [
      {required: true, message: "状态不能为空", trigger: "change"}
    ],
    createBy: [
      {required: true, message: "创建人不能为空", trigger: "blur"}
    ],
    createTime: [
      {required: true, message: "创建时间不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询课程列表 */
function getList() {
  loading.value = true;
  listCourse(queryParams.value).then(response => {
    courseList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

const replacedTableData = computed(() => {
  return courseList.value.teacherId.map(row => {
    const teacherList = teacherList.userId.find(o => row.teacherId === o.userId);
    return {...row, teacherId: teacherList ? teacherList.userName : row.teacherId};
  });
});

function getName(row, column, cellValue) {
  return cellValue;
}

function getTeacherList() {
  listTeacher().then(response => {
    teacherList = response.data
    console.log(teacherList)
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    description: null,
    type: null,
    number: null,
    classroom: null,
    teacherId: null,
    score: null,
    monday: null,
    tuesday: null,
    wednesday: null,
    thursday: null,
    friday: null,
    status: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("courseRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加课程";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getCourse(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改课程";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["courseRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCourse(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCourse(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除课程编号为"' + _ids + '"的数据项？').then(function () {
    return delCourse(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}


function handleSelect(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否选择"' + _ids + '"的数据项？').then(function () {
    return selectCourse(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("选课成功");
  }).catch(() => {
    proxy.$modal.msgSuccess("选课失败");
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('course/course/export', {
    ...queryParams.value
  }, `course_${new Date().getTime()}.xlsx`)
}

getList();
getTeacherList();
</script>
